"""
关注加油站
"""
import os
import sys
import django
import decimal

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(BASE_DIR)
sys.path.insert(0, BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'finance_c2c.settings'
django.setup()

from apps.czb.models import GasFollow
from apps.czb.client import get_czb_api
from core.wchat.wx_base import WxBase
from django.contrib.auth import get_user_model
from django.db.models import Q

wx_base = WxBase()
UserModel = get_user_model()

czb_client = get_czb_api("youpinhuiyuan")


def gas_follow_message():
    # 加油
    gas_follow_queryset = GasFollow.objects.filter(
        Q(user__company__sign="youpinhuiyuan") | Q(user__company__sign="xiaoyou"), user__wx_user__isnull=False)
    users = gas_follow_queryset.values("user").distinct()
    for i in users:
        user = UserModel.objects.get(id=i.get("user"))
        openid = user.wx_user.openid
        gas_all = GasFollow.objects.filter(user=user).all()  # 根据不同用户查价格，所以找出不同的用户来
        gasIds = ""
        for gas in gas_all:
            gasIds += gas.gas.gasId + ","
        gasIds = gasIds[:-1]
        response = czb_client.query_price_by_phone(gasIds, user.mobile)  # 查询出来实时价格
        result = response.get("result")
        content = "您关注的站点价格有变动：\n"
        message = False
        for r in result:
            line_status = False
            gasId = r.get("gasId")
            gasName = r.get("gasName")
            line1 = "\n油\t\t\t\t\t\t\t\t站：%s\n" % gasName
            oilPriceList = r.get("oilPriceList")
            line2 = "变动价格："
            num = 0
            for o in oilPriceList:
                oilNo = o.get("oilNo")
                priceYfq = o.get("priceYfq")
                follow_queryset = gas_all.filter(oilNo=oilNo, gas__gasId=gasId)
                if follow_queryset:
                    follow_obj = follow_queryset.first()
                    if follow_obj.price != decimal.Decimal(priceYfq):
                        different = follow_obj.price - decimal.Decimal(priceYfq)
                        if different > 0:
                            different_value = "比昨日降%s元" % abs(different)
                        else:
                            different_value = "比昨日涨%s元" % abs(different)
                        line_status = True
                        message = True
                        follow_obj.price = priceYfq
                        follow_obj.save()
                        url = "https://oss.kalaibao.com/onlineOil/index.html#/pages/oil_subPackages/oil-confirm?gasId=%s&distance=14.77&oilType=1&oilNum=%s" % (
                            gasId, oilNo)
                        if num > 0:
                            line2 += "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<a href='%s'>%s#；%s元</a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t%s\n" % (
                                url, oilNo, priceYfq, different_value)
                        else:
                            line2 += "<a href='%s'>%s#；%s元</a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t%s\n" % (
                            url, oilNo, priceYfq, different_value)
                        num += 1
            if line_status :
                content = content + line1 + line2
        if message:
            try:
                print(openid)
                print(wx_base.appid)
                wx_base.client.message.send_text(openid, content)
            except Exception as e:
                print(e)
                pass


if __name__ == '__main__':
    gas_follow_message()
